//
//  TabBarViewController.m
//  CC
//
//  Created by Calvin on 4/19/15.
//  Copyright (c) 2015 Calvin. All rights reserved.
//

#import "TabBarViewController.h"
#import "HomeTableViewController.h"
#import "MessageViewController.h"
#import "ActionViewController.h"
#import "DiscoverViewController.h"
#import "ProfileViewController.h"
#import "UIImage+Extension.h"
#import "NavigationViewController.h"
#import "AddTabBar.h"
#import "ComposeViewController.h"
#import "UnReadCountParam.h"
#import "UnReadCountResult.h"
#import "AccountTool.h"
#import "AccountModel.h"
#import "UserTool.h"

@interface TabBarViewController()<UITabBarControllerDelegate,AddTabBarDelegate>
@property (nonatomic,weak) HomeTableViewController *home;
@property (nonatomic,weak) MessageViewController *message;
@property (nonatomic,weak) ProfileViewController *profile;
@property (nonatomic,weak) UIViewController *lastSelectedViewContoller;
@end

@implementation TabBarViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    self.delegate=self;
    [self addAllChildBar];
    [self addCustomBar];
//    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(getUnreadCount) userInfo:nil repeats:YES];
//    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[HomeTableViewController class]]) {
        if (self.lastSelectedViewContoller==viewController) {
            [self.home refresh:YES];
        }
        else
        {
            [self.home refresh:NO];
        }
    }
    self.lastSelectedViewContoller=viewController;
}

-(void)getUnreadCount
{
    UnReadCountParam *param = [UnReadCountParam param];
    param.uid = [AccountTool accountModel].uid;
    [UserTool unreadCountWithParam:param success:^(UnReadCountResult *result) {
        if (result.status==0) {
            self.home.tabBarItem.badgeValue=nil;
        }
        else
        {
            self.home.tabBarItem.badgeValue=[NSString stringWithFormat:@"%d",result.status];
        }
        
        if (result.messageCount==0) {
            self.message.tabBarItem.badgeValue = nil;
        }
        else
        {
            self.message.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",result.messageCount];
        }
        
        if (result.follower==0) {
            self.profile.tabBarItem.badgeValue = 0;
        }
        else
        {
            self.profile.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",result.follower];
        }
        
        [UIApplication sharedApplication].applicationIconBadgeNumber=result.totalCount;
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)addAllChildBar
{
    HomeTableViewController *home = [[HomeTableViewController alloc] init];
    [self addOneChild:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"];
    self.lastSelectedViewContoller=home;
    MessageViewController *message =[[MessageViewController alloc] init];
    [self addOneChild:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"];
    
    DiscoverViewController *discover =[[DiscoverViewController alloc] init];
    [self addOneChild:discover title:@"发现" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];
    
    ProfileViewController *profile =[[ProfileViewController alloc] init];
    [self addOneChild:profile title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];
}

-(void)addCustomBar
{
    AddTabBar *customTabBar = [[AddTabBar alloc]init];
    customTabBar.delegate=self;
    [self setValue:customTabBar forKeyPath:@"tabBar"];
}

-(void)tabBarDidClickedPlusButton:(AddTabBar *)tabBar
{
    ComposeViewController *composeViewController = [[ComposeViewController alloc] init];
    NavigationViewController *navigateController = [[NavigationViewController alloc] initWithRootViewController:composeViewController];
    [self presentViewController:navigateController animated:YES completion:nil];
//    [self.navigationController presentViewController:composeViewController animated:YES completion:nil];
}

-(void) addOneChild:(UIViewController *)child title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *) selectImageName{
//    [child.view setBackgroundColor:RandomColor];
    child.tabBarItem.title=title;
    child.tabBarItem.image =[UIImage imageWithName:imageName];
    
    UIImage *selectedImage =[UIImage imageWithName:selectImageName];
    NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];
    textAttrs[UITextAttributeTextColor]=[UIColor blackColor];
    textAttrs[UITextAttributeFont]=[UIFont systemFontOfSize:10];
    [child.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    NSMutableDictionary *selectedtextAttrs=[NSMutableDictionary dictionary];
    selectedtextAttrs[UITextAttributeTextColor]=[UIColor orangeColor];
    [child.tabBarItem setTitleTextAttributes:selectedtextAttrs forState:UIControlStateSelected];
    if (iOS7) {
        // 声明这张图片用原图(别渲染)
        selectedImage =[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    child.tabBarItem.selectedImage =selectedImage;
    NavigationViewController *navigate =[[NavigationViewController alloc] initWithRootViewController:child];
    
    [self addChildViewController:navigate];
}
@end
